package RefectDemo;


import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @Author: Lenovo
 * @Date: 2024/8/8 7:00
 * @description:
 */
public class Test {
    public static void reflectNewInstance(){
        try {
            Class<?>  c1 = Class.forName("RefectDemo.Student");
            Student student = (Student) c1.newInstance();
            System.out.println(student);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }

    }

    public static void reflectPrivateConstructor(){
        try {
            Class<?> c1 = Class.forName("RefectDemo.Student");
            Constructor<Student> constructor = (Constructor<Student>) c1.getDeclaredConstructor(String.class,int.class);
            constructor.setAccessible(true);
            Student student =  constructor.newInstance("zhangsan",20);
            System.out.println(student);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }

    }

    public static void reflectPrivateField(){
        try {
            Class<?> c1 = Class.forName("RefectDemo.Student");
            Field field = c1.getDeclaredField("name");
            field.setAccessible(true);
            Student student = (Student) c1.newInstance();
            field.set (student,"poll");
            System.out.println(student);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (NoSuchFieldException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }
    public static void reflectPrivateMethod(){
        try {
            Class<?> c1 = Class.forName("RefectDemo.Student");
            Method method = c1.getDeclaredMethod("function", String.class);
            // method.setAccessible(true); 这行代码的作用是设置方法的可访问性标志为 true ，以绕过 Java 的访问控制检查。
            //默认情况下，如果一个方法是私有的，直接访问可能会导致访问异常。但通过将其可访问性设置为 true ，可以在运行时访问和调用私有方法。
            // 然而，这种操作应该谨慎使用，因为它违反了正常的封装原则，可能导致代码的可维护性和安全性问题。
            method.setAccessible(true);
            Student student = (Student) c1.newInstance();
            method.invoke(student,"我是一个参数");
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }

    }

    public static void main(String[] args) {
        //reflectPrivateMethod();
       // reflectPrivateField();
       // reflectPrivateConstructor();
        reflectNewInstance();
    }
    public static void main1(String[] args) {
        //获得Class对象的三种方式
        try {
            Class<?> c1 = Class.forName("RefectDemo.Student");
            Class c2 = Student.class;
            Student student = new Student();
            Class<?> c3 = student.getClass();
            System.out.println(c1.equals(c2));
            System.out.println(c1.equals(c3));
            System.out.println(c2.equals(c3));
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
}
